const app = getApp();

Page({
    data: {
        enterpriseNameVal:"",
        staffNumVal:"",
        licenseVal:"",
        webUrlVal:'',
        addressVal:"",
        taskid:"",
        id:"",
        subid:"",
        date:"",
        aresname:'',
        areaShow:false,
        areaList:[],
        industryBool:false,
        teamShow:false,
        role_type:null,
        roleName:'',
        industryData:{
            minIndex:0,
            name:'',
            id:'',
        },
        team:[
            {
                name:'企业',
                value:1
            },
            {
                name:'高校',
                value:2
            }
        ],
        columns: [],
        industryList:[],
        mainActiveIndex:0,
        qualification:[],
        qualificationId:[],
        fileList:[],
        dateShow:false,
        currentDate: new Date().getTime(),
        minDate: new Date(1949,8,31).getTime(),
        formatter(type, value) {
            if (type === 'year') {
                return `${value}年`;
            }
            if (type === 'month') {
                return `${value}月`;
            }
            if(type === 'day'){
                return `${value}日`;
            }
            return value;
        },
    },
    onLoad: function (options) {
        this.industryList()
        this.areaList()
        this.authInfo()
    },

    teamClick(){
        this.setData({
            teamShow:true
        })
    },

    teamClose(){
        this.setData({
            teamShow:false
        })
    },

    teamSelect(e){
        console.log(e,'teamSelect')
        let {role_type,roleName}=this.data
        role_type=e.detail.value
        roleName=e.detail.name
        this.setData({
            role_type,
            roleName
        })
        this.teamClose()
    },

    // r认证信息
    authInfo:function(){
        var _this = this;
        wx.showLoading({
            title: '加载中',
        })
        wx.request({
            url: app.globalData.baseUrl + 'auth/reAuthInfo',
            data: {
                token: app.globalData.token
            },
            success(res) {
                const {enterPrise}=res.data.data.enterPriseAuth
                if (res.data.code == 1000) {
                    if(enterPrise.status===2){
                        let imgUrl=[]
                        let imgId=[]
                        enterPrise.attachement.forEach(res=>{
                            imgUrl.push(
                                {
                                    url:res.url,
                                    isImage: true,
                                }
                            )
                            imgId.push(res.id)
                        })
                        _this.setData({
                            enterpriseNameVal:enterPrise.company_name,
                            industry:enterPrise.cate,
                            staffNumVal:enterPrise.employee_num,
                            licenseVal:enterPrise.business_license,
                            date:enterPrise.begin_at,
                            webUrlVal:enterPrise.website,
                            aresname:enterPrise.province+'-'+enterPrise.city,
                            addressVal:enterPrise.address,
                            id:enterPrise.city_id,
                            subid:enterPrise.province_id,
                            taskid:enterPrise.cate_id,
                            qualification:imgUrl,
                            qualificationId:imgId
                        })
                    }
                    wx.hideLoading()
                } else {
                    wx.showToast({
                        title: res.data.message,
                        icon: 'none',
                        duration: 2000
                    })
                }
            }
        })
    },


    onClickNav({ detail = {} }) {
        this.setData({
            mainActiveIndex: detail.index || 0,

        });
    },

    onClickItem({ detail = {} }) {
        this.setData({
            activeId:detail.id,
            ['industryData.name']: detail.name,

        });
    },

    industryClick(){
        this.setData({
            industryBool: true
        })
    },

    industryClose(){
        const {minIndex,name,id}=this.data.industryData
        this.setData({
            industryBool: false,
            mainActiveIndex:minIndex,
            activeId:id,
        })
    },

    industryChange(){
        const {mainActiveIndex,activeId,industryData}=this.data
        this.setData({
            industry:industryData.name,
            taskid:activeId,
            ['industryData.minIndex']:mainActiveIndex,
            ['industryData.id']: activeId,
            industryBool: false,
        })
    },


    /**
     * 行业列表
     */
    industryList(){
        var _this = this;
        wx.showLoading({
            title: '加载中',
        })
        wx.request({
            url: app.globalData.baseUrl + 'taskCate',
            data: {},
            success(res) {
                wx.hideLoading()
                if (res.data.code == 1000) {
                    let dataList=res.data.data
                    dataList?.forEach(item=>{
                        item['text']=item.name
                        item['children']=item.children_task
                        item['children']?.forEach(res=>{
                            res['text']=res.name
                        })
                    })
                    _this.setData({
                        industryList: dataList
                    })
                } else {
                    wx.showToast({
                        title: res.data.message,
                        icon: 'none',
                        duration: 2000
                    })
                }
            }
        })
    },

    beforeRead(e){
        const {file}=e.detail
        if(file.size<3145728){
            let { qualification, qualificationId } = this.data
            let _this = this;
            wx.showLoading({
                title: '加载中',
            })
            wx.uploadFile({
                url: app.globalData.baseUrl + 'fileUpload',
                filePath: file.url,
                name: 'file',
                formData: {
                    token: app.globalData.token,
                    file: file.url
                },
                success(upres) {
                    wx.hideLoading()
                    let obj = JSON.parse(upres.data)
                    let data={
                        url:obj.data.url,
                        isImage: true,
                    }
                    qualification.push(data)
                    qualificationId.push(obj.data.id)
                    _this.setData({
                        qualification: qualification,
                        qualificationId: qualificationId
                    })
                }
            })
        }else {
            wx.showToast({
                title: '图片不能大于3M',
                icon: 'none',
                duration: 2000
            })
            // callback(false);
        }
    },

    uploadImg(e){
        const { file } = e.detail;

    },
    /**
     * 删除图片
     * @param e
     */
    delImg(e){
        console.log(e,'eeeeeeeeeeeeeeee')
        const {index}=e.detail
        let { qualification, qualificationId } = this.data
        qualification.splice(index,1)
        qualificationId.splice(index,1)
        this.setData({
            qualification,
            qualificationId
        })
    },


    webUrlChange(){
        wx.navigateTo({
            url: '/userPage/pages/storeText/storeText?type=2&text='+this.data.webUrlVal,
        })
    },


    /**
     * 获取地址列表
     */
    areaList(){
        let _this = this;
        wx.showLoading({
            title: '加载中',
        })
        wx.request({
            url: app.globalData.baseUrl + 'task/district',
            data: {},
            success(res) {
                wx.hideLoading()
                if (res.data.code == 1000) {
                    let list=_this.areaListHand(res.data.data)
                    let listKey=Object.keys(list)
                    let columns=[
                        {
                            values: listKey,
                            className: 'column1',
                        },
                        {
                            values: list[listKey[0]],
                            className: 'column2',
                            defaultIndex: 2,
                        },
                    ]
                    _this.setData({
                        areaList: list,
                        columns,
                    })
                } else {
                    wx.showToast({
                        title: res.data.message,
                        icon: 'none',
                        duration: 2000
                    })
                }
            }
        })
    },

    areaListHand(data) {
        let list = {}
        data.forEach(item=>{
          list[item.name]=item.child
        })
        return list
    },




    // 选择地址
    areaBtn: function () {
        this.setData({
            areaShow:true
        })
    },

    onChange(event) {
        const { picker, value, index } = event.detail;
        picker.setColumnValues(1, this.data.areaList[value[0]]);
    },

    areaCancel(){
        this.setData({
            areaShow:false
        })
    },

    areafirm(e){
        let data=e.detail.value
        let aresname
        aresname=data[0]+'-'+data[1].name
        this.setData({
            id:data[1].id,
            subid:data[1].upid,
            aresname
        })
        this.areaCancel()
    },



    // 企业认证
    enterpriseCertificationBtn:function(){
        var url = app.globalData.baseUrl + 'user/enterpriseAuth';
        var { enterpriseNameVal, role_type,staffNumVal, licenseVal, webUrlVal, addressVal, taskid, id, subid, date, qualificationId } = this.data;
        if(!role_type){
            wx.showToast({
                title: '请选择团队类别',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if(!enterpriseNameVal){
            wx.showToast({
                title: '请输入企业/高校名称',
                icon: 'none',
                duration: 2000
            })
            return
        }
        if(!licenseVal){
            wx.showToast({
                title: '请填写统一社会信用代码',
                icon: 'none',
                duration: 2000
            })
            return
        }

        let idString=qualificationId.toString()
        var data = {
            company_name: enterpriseNameVal,
            cate_id: taskid,
            employee_num: staffNumVal,
            business_license: licenseVal,
            begin_at: date,
            province: subid,
            role_type,
            city: id,
            website:webUrlVal,
            address: addressVal,
            file_id: idString,
            token: app.globalData.token
        }
        wx.showLoading({
            title: '加载中',
        })
        app.wxPostRequest(url, data, (res) => {
            wx.hideLoading()
            console.log(res)
            if (res.code == 1000) {
                wx.showToast({
                    title: '认证提交成功',
                    icon: 'success',
                    duration: 2000
                })
                setTimeout(function(){
                    wx.navigateBack({
                        delta: 1
                    })
                },2000)
            }else{
                wx.showToast({
                    title: res.message,
                    icon: 'none',
                    duration: 2000
                })
            }
        }, (err) => {
            wx.hideLoading()
            wx.showToast({
                title: err.data.message,
                icon: 'none',
                duration: 2000
            })
        })
    },

    formatDate: function (value) {//调用时间戳为日期显示
        let date = new Date(value)
        let y = date.getFullYear()  //获取年份
        let m = date.getMonth() + 1  //获取月份
        m = m < 10 ? "0" + m : m  //月份不满10天显示前加0
        let d = date.getDate()  //获取日期
        d = d < 10 ? "0" + d : d  //日期不满10天显示前加0
        return y + "-" + m + "-" + d
    },



    dataShowChange(){
        this.setData({
            dateShow: true,
        });
    },
    onInput(event) {
        let date=this.formatDate(event.detail)
        this.setData({
            date,
        });
        this.onClose()
    },
    onClose(){
        this.setData({
            dateShow: false,
        });
    },
});
